<?php


namespace app\admin\controller;

use app\common\controller\Backend;

class Master extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Master');
    }

    /*查看*/
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();

    }

    /*添加*/
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $this->model->create($params);
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /*删除*/
    public function del($ids = "")
    {
        if ($ids) {
            $count = $this->model->where('id', 'in', $ids)->delete();
            if ($count) {
                $this->success();
            }

        }
    }

    /*编辑*/
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $row->save($params);
                $this->success();
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /*批量*/
    public function multi($ids = "")
    {
    }

    //根据orderid获取orderinfo的所有商品信息
    public function getGoodsByOrderId($order_id)
    {
        $order_info = model('OrderInfo')->where('order_id', $order_id)->select();
        $product_info = [];
        foreach ($order_info as $k => $v) {
            $product_name = model('Product')->field('product_name')->where('id', $v['product_id'])->find();
            $design = model('UserDesign')->field('design_data')->where('id', $v['design_id'])->find();
            $product_info[$k]['product_name'] = $product_name['product_name'];
            $product_info[$k]['design'] = $design['design_data'];
            $product_info[$k]['product_size'] = $v['product_size'];
            $product_info[$k]['buy_num'] = $v['buy_num'];
        };
        return $product_info;
    }


}
